//
//  TabbarViewController.swift
//  CXSwiftDemo
//
//  Created by 潮汐 on 2023/11/28.
//  Copyright © 2023 小潮汐. All rights reserved.
//

import UIKit

class TabbarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        
        setValue(TabBar(), forKeyPath: "tabBar")//系统自带的tabBar属性是只读的，用kvo把系统的tabBar替换为自己封装的TabBar，方便修改一些东西
//        tabBar.tintColor = UIColor.black//设置tabbar的item的颜色
        tabBar.barTintColor = UIColor.white//设置tabbar背景色
        tabBar.backgroundColor = UIColor.white//设置tabbar背景颜色-有效
//        print(tabBar)//<TestQiushi.TabBar: 0x7f920bf09680; baseClass = UITabBar; frame = (0 795; 390 49); autoresize = W+TM; layer = <CALayer: 0x600002385d40>> 说明更换成功
        
        
        addChild("糗事", "tabbar_home", "tabbar_homeS", CXHomeViewController.self)
        addChild("动态", "tabbar_my", "tabbar_myS", TrendViewController.self)
        addChild("直播", "tabbar_home", "tabbar_homeS", FiveViewController.self)
    }
    
    func addChild(_ title:String,
                  _ image:String,
                  _ selectedImage:String,
                  _ type:UIViewController.Type) {
        //导航栏
        let child = CXUINavigationController(rootViewController: type.init())
       //标题
        child.title = title
        //常规图片
        child.tabBarItem.image = UIImage(named: image)
        //选中图片
        child.tabBarItem.selectedImage = UIImage(named: selectedImage)
        //设置标题选中颜色
        child.tabBarItem.setTitleTextAttributes([
            NSAttributedString.Key.foregroundColor:UIColor.red
        ], for: .selected)//设置文字被选中时的颜色
        //图片在Assets里边直接设置，如果不设置，会默认为系统自动填充颜色为蓝色，可以设置原图色
        addChild(child)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
